package com.yisu.auction.manager.core.service.impl;

import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.entity.SystemNotice;
import com.yisu.auction.manager.core.mapper.SystemNoticeMapper;
import com.yisu.auction.manager.core.service.ISystemNoticeService;
import com.yisu.auction.manager.domain.request.SystemNoticePackage.SystemNoticeRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 黄萧林
 * Business Description：
 * createTime：2017/8/31
 */
@Service("ISystemNoticeService")
public class SystemNoticeServiceImpl implements ISystemNoticeService {
    @Resource
    private SystemNoticeMapper systemNoticeMapper;

    /**
     * 分页获取公告信息
     *
     * @param systemNoticeRequest
     * @return
     */
    @Override
    public EasyUIGrid<SystemNotice> getSystemNoticeInfoByPage(SystemNoticeRequest systemNoticeRequest) {
        List<SystemNotice> systemConfigs = systemNoticeMapper.getSystemNoticeInfoByPage(systemNoticeRequest);
        EasyUIGrid<SystemNotice> gridData = new EasyUIGrid<>();
        gridData.setTotal(systemNoticeRequest.getTotalRecords());
        gridData.setRows(systemConfigs);
        return gridData;
    }

    /**
     * 修改公告信息
     *
     * @param systemNotice
     * @return
     */
    @Override
    public boolean modifySystemNoticeInfo(SystemNotice systemNotice) {
        SystemNotice systemNotice1 = systemNoticeMapper.selectByPrimaryKey(systemNotice.getId());
        if (systemNotice1 == null) {
            SystemNotice systemNotice2 = new SystemNotice();
            systemNotice2.setCreateDate(new Date());
            systemNotice2.setId(systemNotice.getId());
            systemNotice2.setTitle(systemNotice.getTitle());
            systemNotice2.setContentAbstract(systemNotice.getContentAbstract());
            systemNotice2.setToMemberId(systemNotice.getToMemberId());
            systemNotice2.setMsgType(systemNotice.getMsgType());
            systemNotice2.setTypeIco(systemNotice.getTypeIco());
            systemNotice2.setTypeName(systemNotice.getTypeName());
            systemNotice2.setContent(systemNotice.getContent());
            return systemNoticeMapper.insertSelective(systemNotice2) > 0;
        } else {
            systemNotice1.setUpdateDate(new Date());
            systemNotice1.setId(systemNotice.getId());
            systemNotice1.setTitle(systemNotice.getTitle());
            systemNotice1.setContentAbstract(systemNotice.getContentAbstract());
            systemNotice1.setToMemberId(systemNotice.getToMemberId());
            systemNotice1.setMsgType(systemNotice.getMsgType());
            systemNotice1.setTypeIco(systemNotice.getTypeIco());
            systemNotice1.setTypeName(systemNotice.getTypeName());
            systemNotice1.setContent(systemNotice.getContent());
            return systemNoticeMapper.updateByPrimaryKeySelective(systemNotice1) > 0;
        }
    }

    @Override
    public boolean deleteSystemNoticeInfo(Long id) {
        return systemNoticeMapper.deleteByPrimaryKey(id)>0;
    }

    @Override
    public SystemNotice getSystemNotice(Long id) {
        return systemNoticeMapper.selectByPrimaryKey(id);
    }
}
